package io.renren.modules.package_storage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.CommonUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.goods_cate.entity.V9GoodsCateEntity;
import io.renren.modules.goods_cate.service.V9GoodsCateService;
import io.renren.modules.member.entity.V9MemberEntity;
import io.renren.modules.member.service.V9MemberService;
import io.renren.modules.order.entity.V9OrderEntity;
import io.renren.modules.order.service.V9OrderService;
import io.renren.modules.package_storage.entity.V9PackageStorageEntity;
import io.renren.modules.package_storage.service.V9PackageStorageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 货运管理
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-09-21 09:38:52
 */
@RestController
@RequestMapping("package_storage/v9packagestorage")
public class V9PackageStorageController {
	@Autowired
	private V9PackageStorageService v9PackageStorageService;
	@Autowired
	private V9OrderService v9OrderService;
	@Autowired
	private V9MemberService v9MemberService;
	@Autowired
	private V9GoodsCateService v9GoodsCateService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("package_storage:v9packagestorage:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = v9PackageStorageService.queryPage(params);

		return R.ok().put("page", page);
	}

	@ResponseBody
	@RequestMapping("/getPackage")
	public List<V9PackageStorageEntity> getPackage(@RequestParam Map<String, Object> params) {
		String order_id = (String) params.get("order_id");
		Map<String, Object> columnMap = new HashMap<>();
		columnMap.put("order_id", order_id);
		List<V9PackageStorageEntity> listByMap = (List<V9PackageStorageEntity>) v9PackageStorageService
				.listByMap(columnMap);

		return listByMap;
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("package_storage:v9packagestorage:info")
	public R info(@PathVariable("id") Integer id) {
		V9PackageStorageEntity v9PackageStorage = v9PackageStorageService.getById(id);
		List<V9GoodsCateEntity>v9GoodsCateEntities=v9GoodsCateService.list();
		return R.ok().put("v9PackageStorage", v9PackageStorage).put("goodscate", v9GoodsCateEntities);
	}
	@RequestMapping("/goodscate")
	public R goodscate() {
		List<V9GoodsCateEntity>v9GoodsCateEntities=v9GoodsCateService.list();
		return R.ok().put("rst", 1).put("goodscate", v9GoodsCateEntities);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("package_storage:v9packagestorage:save")
	public R save(@RequestBody V9PackageStorageEntity v9PackageStorage) {
		v9PackageStorage.setAddTime(CommonUtils.getCurrentTimeStamp());
		v9PackageStorageService.save(v9PackageStorage);
		return R.ok();
	}
	/**
	 * 保存
	 */
	@RequestMapping("/save2")
	@RequiresPermissions("package_storage:v9packagestorage:save")
	public R save2(@RequestBody V9PackageStorageEntity v9PackageStorage) {
		String userCode = v9PackageStorage.getuCode();
		List<V9OrderEntity> listOrder = v9OrderService.list(new QueryWrapper<V9OrderEntity>().eq("special_id", userCode)
				.eq("order_status", 0).orderByDesc("order_id"));
		if (listOrder.size() > 0) {
			Integer orderId = listOrder.get(0).getOrderId();
			v9PackageStorage.setOrderId(orderId);
		} else {
			V9OrderEntity orderEntity = new V9OrderEntity();
			String order_sn = CommonUtils.createOrderSn();
			orderEntity.setOrderSn(order_sn);
			orderEntity.setSpecialId(userCode);
			//用户ID
			V9MemberEntity memberEntity=v9MemberService.getOne(new QueryWrapper<V9MemberEntity>().eq("username", userCode));
			orderEntity.setUid(memberEntity.getUserid());
			v9OrderService.save(orderEntity);
			orderEntity = v9OrderService.getOne(new QueryWrapper<V9OrderEntity>().eq("order_sn", order_sn));
			Integer orderId = orderEntity.getOrderId();
			v9PackageStorage.setOrderId(orderId);
		}
		v9PackageStorageService.save(v9PackageStorage);
		return R.ok();
	}
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("package_storage:v9packagestorage:update")
	public R update(@RequestBody V9PackageStorageEntity v9PackageStorage) {
		v9PackageStorageService.updateById(v9PackageStorage);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("package_storage:v9packagestorage:delete")
	public R delete(@RequestBody Integer[] ids) {
		v9PackageStorageService.removeByIds(Arrays.asList(ids));

		return R.ok();
	}

	/**
	 *
	 * @return
	 *//*
	@ResponseBody
	@RequestMapping("/getkuaidi100")
	public String getPackages(@RequestParam Map<String, Object> params) {
		String type = (String) params.get("type");//
		String postid = (String) params.get("postid");
		String orderSn = (String) params.get("order_sn");
		String key = "zjINYOWH8268";
		//String url = " http://www.kuaidi100.com/query?num=906919164534&key=zjINYOWH8268";
		String url = "https://www.kuaidi100.com/query?type=" + type + "&postid=" + postid;
		return CommonUtils.doGet(url);
	}*/

}
